﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;
using Microsoft.AspNetCore.Mvc.Rendering;
using XZTZ.UMP.Public.Models.Flow;
using XZTZ.UMP.WEB.Data;

namespace XZTZ.UMP.WEB.Pages.Flow.FlowTemplate
{
    public class CreateModel : PageModel
    {
        private readonly XZTZ.UMP.WEB.Data.ApplicationDbContext _context;
        public string errormessage { get; set; }
        public CreateModel(XZTZ.UMP.WEB.Data.ApplicationDbContext context)
        {
            _context = context;
        }

        public IActionResult OnGet()
        {
            return Page();
        }

        [BindProperty]
        public Flow_Template Flow_Template { get; set; }

        public async Task<IActionResult> OnPostAsync()
        {
            if (!ModelState.IsValid)
            {
                return Page();
            }
            Flow_Template.guiVisonID =new Guid(HttpContext.Session.GetString("visionId"));
            Flow_Template.strBusinessTypeID = HttpContext.Session.GetString("bussid");
            if (string.IsNullOrEmpty(Flow_Template.strFlowName))
            {
                errormessage = "版本名称不能为空！";
                return Page();
            }
            int count = _context.FlowVision.Where(a => a.strVisionName.Contains(Flow_Template.strFlowName)).ToList().Count();
            if (count > 0)
            {
                errormessage = "版本名称不能重复！";
                return Page();
            }
            _context.Flow_Template.Add(Flow_Template);
            await _context.SaveChangesAsync();

            return RedirectToPage("./Index");
        }
    }
}